<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册</title>
    <link rel="stylesheet" href="css/conmmon.css">
    <link rel="stylesheet" href="css/login.css">
    <script src="js/jquery.min.js"></script>
    <script src="js/common.js"></script>
</head>

<body>
<!-- 导航栏 -->
<div class="nav">
    <img src="img/logo2.jpg" alt="">
    <span class="title">博客系统</span>
    <span class="spacer"></span>
    <a href="blog_list.html">主页</a>
    <!--    <a href="blog_add.html">写博客</a>-->
    <a href="login.html">登陆</a>
    <!-- <a href="#">注销</a> -->
    <a href="about.html">站长简介</a>
</div>
<!-- 版心 -->
<div class="login-container">
    <!-- 中间的注册框 -->
    <div class="login-dialog" style="height: 500px">
        <h3 style="padding: 35px 0">注册</h3>
        <div class="row">
            <span>昵称</span>
            <input type="text" id="username" placeholder="username">
        </div>
        <div class="row">
            <span>电子邮箱</span>
            <input type="text" id="email" placeholder="email">
        </div>
        <div class="row">
            <span>邮箱验证码</span>
            <input type="text" id="code" placeholder="email-code">
        </div>
        <div class="row">
            <button id="sendemail" onclick="sendcode()">发送验证码</button>
        </div>
        <div class="row">
            <span>密码</span>
            <input type="password" id="password1" placeholder="password">
        </div>
        <div class="row">
            <span>确认密码</span>
            <input type="password" id="password2" placeholder="confirmPassword">
        </div>
        <div class="row">
            <button id="submit" onclick="register()">提交</button>
        </div>
    </div>
</div>
<script>
    function sendcode() {
        var email = jQuery("#email");
        if (email.val() == "") {
            alert("请输入邮箱~");
            email.focus();
            return;
        }
        jQuery.ajax({
            url: "/user/getTextCode",
            type: "POST",
            data: {"email": email.val()},
            success: function (result) {
                alert("验证码已发送，注意查收~");
            }
        });
    }

    function register() {
        var username = jQuery("#username");
        var password1 = jQuery("#password1");
        var password2 = jQuery("#password2");
        var email = jQuery("#email");
        var code = jQuery("#code");
        if (username.val() == "") {
            alert("请输入用户名~");
            username.focus();
            return;
        }
        if (email.val() == "") {
            alert("请输入邮箱~");
            email.focus();
            return;
        }
        if (code.val() == "") {
            alert("请输入验证码~");
            code.focus();
            return;
        }
        if (password1.val() == "") {
            alert("请输入密码~");
            password1.focus();
            return;
        }
        if (password2.val() == "") {
            alert("请输入确认密码~");
            password2.focus();
            return;
        }
        if (password2.val() != password1.val()) {
            alert("两次密码不一致，请重新输入~");
            password2.focus();
            return;
        }
        jQuery.ajax({
            url: "/user/register",
            type: "POST",
            data: {
                "username": username.val(),
                "password": password1.val(),
                "email": email.val(),
                "code": code.val()
            },
            success: function (result) {
                if (result != null && result.code == 200 && result.data == 1) {
                    if (confirm("注册成功！是否跳转登录界面？")) {
                        location.href = "/login.html";
                    }
                } else {
                    if (result.code == -1)
                        alert("注册失败！请稍后再试");
                    if (result.code == -2)
                        alert("验证码错误");
                    if (result.code == -3)
                        alert("用户名已存在");
                    if (result.code == -4)
                        alert("邮箱已注册");
                }
            }
        });
    }
</script>
</body>
</html>